// License.h: interface for the License class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_LICENSE_H__3EEBAB55_D0DD_4F6E_A281_6909C1578F5A__INCLUDED_)
#define AFX_LICENSE_H__3EEBAB55_D0DD_4F6E_A281_6909C1578F5A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <string>

typedef unsigned char BYTE;
typedef unsigned char * LPBYTE;

#define LIC_NUMOFDWORD          4
#define LIC_KEYLEN              (sizeof(DWORD)*LIC_NUMOFDWORD+8)
#define LIC_DEFAULTPERMISSION   7

class LicenseManager
{
public:
	LicenseManager(const std::string& strMacAddr = "", const std::string& strDesc = "", DWORD dwExpiration = 0, DWORD dwEvaluationPeriod = 0);
	virtual ~LicenseManager();

	// Generate license
	bool ChangePermission(int nId, bool bPermit);
	bool ChangeConcurrentNumber(int nId, int nNum);
	void GetKeyString(std::string& str) const;

	// Apply license
	DWORD ApplyKey(const std::string& strKey);

	// Get/Set functions
	void SetMacAddrString(const std::string& str);
	void GetMacAddrString(std::string& str) const;

	void SetMacAddr(LPBYTE lpbyMacAddr) {memcpy(m_byMacAddr, lpbyMacAddr, 6);}
	void GetMacAddr(LPBYTE lpbyMacAddr) {memcpy(lpbyMacAddr, m_byMacAddr, 6);}

	void SetExpiration(DWORD dwExpiration) {m_dwExpiration = dwExpiration;}
	DWORD GetExpiration() const {return m_dwExpiration;}

	void SetEvaluationPeriod(DWORD dwEvaluationPeriod) {m_dwEvaluationPeriod = dwEvaluationPeriod;}
	DWORD GetEvaluationPeriod() const {return m_dwEvaluationPeriod;}

	void SetDescription(const std::string& strDesc) {m_strDesc = strDesc;}
	const std::string& GetDescription() const {return m_strDesc;}

	// Get expiration as string;
	void GetExpirationString(std::string& strExpiration) const;
	// Get evaluation period as string;
	void GetEvaluationPeriodString(std::string& strEvaluationPeriod) const;
	// Get permission information based on the nId;
	bool GetPermission(int nId) const;
	// Get concurrent number based on the nId;
	int GetConcurrentNumber(int nId) const;
	// Get concurrent number as string;
	void GetConcurrentNumberString(int nId, std::string& strConcurrentNumber) const;

	static bool GetLocalMACAddress(std::vector<std::string>& vecMACAddress, std::vector<std::string>& vecDescription);

private:
	bool GenerateKey();

	bool IsExpirationValid() const;
	bool IsEvaluationPeriodValid() const;
	bool IsDateValid(int nYear, int nMonth, int nDay) const;

private:
	BYTE m_byMacAddr[6];
	BYTE m_byConcurrentNumber[3];
	DWORD m_dwPermission; // Bit 0: for data permission(1 if permitted); Bit 1: for curve permission; Bit 2: for report permission;
	DWORD m_dwExpiration; // Low word: year, low byte of high word: day, high byte of high word: month;
	DWORD m_dwEvaluationPeriod; // Low word: year, low byte of high word: day, high byte of high word: month;
	BYTE m_byLicenseKey[LIC_KEYLEN];
	std::string m_strDesc;
};

#endif // !defined(AFX_LICENSE_H__3EEBAB55_D0DD_4F6E_A281_6909C1578F5A__INCLUDED_)
